$(function(){
	
	//从后台读取并填充到页面相应的数据
	init();
	
	// 验证
	validateInit();
	
	//图表
	Morris.Line({
        element: 'morris-one-line-chart',
            data: [
                { year: '2017-10-23', value: 1 },
                { year: '2017-10-24', value: 1 },
                { year: '2017-10-25', value: 3 },
                { year: '2017-10-26', value: 2 },
                { year: '2017-10-27', value: 1 },
                { year: '2017-10-28', value: 2 },
                { year: '2017-10-29', value: 5 }
            ],
        xkey: 'year',
        ykeys: ['value'],
        resize: true,
        lineWidth:4,
        labels: ['Value'],
        lineColors: ['#1ab394'],
        pointSize:5,
    });
	
	showtime();
});


function init() {
	//用户信息
	$.ajax({
		url:"user/getLoginUser.action",
		type:"post",
		success: function(data) {
			//console.log(data);
			//登录信息
			$("#userNameSpan").html(data.userName);
			$("#userNameSpan2").html(data.userName);//。。。。。
			$("#roleSpan").html(getRoleNames(data.id));
			$("#loginNameSpan").html(data.loginName);
			$("#lastLoginTimeSpan").html(moment(data.lastLoginTime).format('YYYY-MM-DD HH:mm:ss'));
			$("#loginCountSpan").html(data.loginCount);
			$("#passwordExpireSpan").html(data.passwordExpireTime);
			
			//个人资料
			$("#userNameSpan").html(data.userName);
			$("#userAddressSpan").html(data.userAddress);
			$("#userDescSpan").html(data.userDesc);
			
			//修改密码模态框
			$("#updatePasswordModal [name='id']").val(data.id);
		},
		error: function() {
			toastr.error("加载登录信息出错，请联系管理员");
		}
	});
	
	//ip地址查询
	$.ajax({
		url: "common/getClientIp.action",
		type: "post",
		success: function(data) {
			$("#ipSpan").html(data);
		},
		error: function() {
			toastr.error("获取ip地址出错，请联系管理员");
		}
	});
	
}
function validateInit() {


	$("#updatePasswordForm").validate(
		{
			rules : {
				userPassword : {
					required : true,
					minlength : 6,
					remote : {
						url : "user/userCheckRepect.action",
						type : "post",
						data : {
							"id": function() {
								return $("#updatePasswordForm [name='id'").val();
							},
							"userPassword": function() {
								return $("#updatePasswordForm [name='userPassword'").val();
							}
						}
					}
				} 
			},
			messages : {
				userPassword : {
					remote : "密码不能和原密码相同"
				}
			},
			success : function(label, element) {// error是lavel标签，element是input标签
				labelReset(element);

				var myTipLabel = '<i class="glyphicon glyphicon-ok form-control-feedback my-tip-i"></i>';
				element.parent().append(myTipLabel);
				element.parent().parent().addClass('has-success');
			},
			errorPlacement : function(label, element) {
				labelReset(element);

				var myTipLabel = '<i class="glyphicon glyphicon-remove form-control-feedback my-tip-i"></i>'
						+ '<label class="control-label my-tip-label">'
						+ label.text() + '</label>';
				element.addClass("error");
				element.parent().append(myTipLabel);
				element.parent().parent().addClass('has-error');
			}
		});
}

function timeFormat(time) {
    return time < 10 ? '0' + time: time;
}
function showtime(){
	
	var date = new Date();
	

	var now=date.getFullYear()+'-'+ timeFormat(date.getMonth()+1) +"-"+ timeFormat(date.getDate()) 
				+" "+ timeFormat(date.getHours()) +':'+ timeFormat(date.getMinutes())+":"+ timeFormat(date.getSeconds());
	$("#showTimeSpan").html(now);
	//console.log(now);
	setTimeout("showtime()",1000);
}

function updatePassword() {
	formReset("#updatePasswordForm");
	$("#updatePasswordModal").modal("show");
}

function updatePasswordSave() {
	if(!$("#updatePasswordForm").valid()){
		return false;
	}
	$.ajax({
		url : "user/update.action",
		type : "post",
		data : $("#updatePasswordForm").serialize(),
		success : function(data) {
			if (data.success == "true") {
				toastr.success("保存成功");
			} else {
				toastr.error("提交数据库发生错误，请联系管理员");
			}
		},
		error : function() {
			toastr.error("提交后台发生错误，请联系管理员");
		}
	});

	$("#updatePasswordModal").modal("hide");
}

function updateUserInfo() {
	formReset("#updateUserInfoForm");
	$("#updateUserInfoModal").modal("show");
}